import path from 'path'
import { formatVueFile } from './format/formatFile.js'
import shell from 'shelljs'
import { glob } from 'glob'
import { fileSavePath } from './config/importOrigin.js'

const getAllVueFile = async () => {
  const vueFileList = await glob('**/*.vue', { ignore: '**/*-formatted.vue' })
  return Promise.resolve(vueFileList)
}
const main = async () => {
  const { code: beforeEslintCode } = shell.exec('pnpm run eslint')
  if (beforeEslintCode !== 0) throw new Error('eslint格式化失败')

  const { code: beforeFormatCode } = shell.exec('pnpm run untransformed-file-format')
  if (beforeFormatCode !== 0) throw new Error('prettier格式化失败')

  const vueFileList = await getAllVueFile()
  vueFileList.forEach((file) => {
    formatVueFile(`${path.resolve()}/${file}`)
  })

  let eslintName = `transformed-file-format`
  if (fileSavePath && fileSavePath !== 'replace') eslintName = 'transformed-file-format-with-path'
  const { code: afterFormatCode } = shell.exec(`pnpm run ${eslintName}`)
  if (afterFormatCode !== 0) throw new Error('转换后eslint格式化失败')
}

// main()

const debug = () => {
  formatVueFile(`${path.resolve()}/Example.vue`)
}

debug()
